/**
 * jquery fn easeup
 * 
 * 结构:
 * <div id=''>
 * 		//everything you want.
 * </div>
 * @param
 * options = {
 * 		speed: 1000 //(pix/s)滚动速度(保持速度不变)
 * }
 * @author  tanggf
 */
(function($){
	$.fn.easeup = function(options){
		//default setting
		options = $.extend({			
			speed: 1000
		}, options);
		
		return this.each(function(){
			_get_easeup_(this, options);
		});	
	};
	
	function _get_easeup_(ele, options){
		var $this = $(ele);
		var obj = $this.data("_wt_easeup_");
		if( ! obj){
			obj = new _EaseuP_(ele, options);
			$this.data("_wt_easeup_", obj);
		}
		return obj;
	}
	
	function _EaseuP_(ele, options){
		this.$ele = $(ele);
		this.options = options;
		this._init_();
	}
	
	_EaseuP_.prototype = {
		_init_: function(){
			this.visible = false;
			this.scrolling = false;
			
			var self = this;		
			this.$ele.click(function(){
				self._fade_away();
				self._jump_to_top();
			});
			
			$(window).scroll(function(){
				if(self.scrolling) {
					return true;
				}
				self._scrolling_();
			});
		},
		
		_jump_to_top: function(){
			var top = $(window).scrollTop();
			//计算时间
			var duration =parseInt((top / this.options.speed) * 1000);
			var self = this;
			$("html, body").animate({
				'scrollTop': 0
			}, duration, function(){
				self.scrolling = false;
			});
			
			self._no_op();
		},
		
		_fade_away: function(){
			this.scrolling = true;
			this.visible = false;
			this.$ele.fadeOut();
		},
		
		_scrolling_: function(){
			var top = $(window).scrollTop();
			if(top == 0){
				this.visible = false;
				this.$ele.fadeOut();
			}else if( ! this.visible){
				this.visible = true;
				this.$ele.fadeIn();
			}
		},
		
		_no_op: function(){}
	};
})(jQuery);